<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
        //echo 'shouye';
        //连表查询出精贴或置顶的数据 显示在首页
        $sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.tip<>'0'";
        $tips = M()->query($sql);
        //var_dump($tips);exit;
        $this->assign('tips',$tips);


        //分页
        $question = M('question'); // 实例化question对象
        $count = $question->where("tip='0'")->count();// 查询满足要求的总记录数 
        //var_dump($count); //string '14' (length=2)
        $Page = new \Think\Page($count,4);// 实例化分页类 传入总记录数和每页显示的记录数(4)
        $show = $Page->show();// 分页显示输出 show方法生成页码
        //echo $show; //<< 1234 >> http://127.0.0.1/yuephp/bbs1/index.php/Home/Index/index/p/3.html
        $this->assign('page',$show);
        //firstRow-0,4,8;listRows-4 从第(8)条后面取多少(4)条 根据传的P生成不一样的值
        var_dump($Page->firstRow,$Page->listRows);



        //连表查询出普通帖子的数据 显示在首页
        //limit ".$Page->firstRow.','.$Page->listRows 查询分页需要的数据 firstRow-0,4,8;listRows-4,从第(8)条后面取多少(4)条 根据传的P生成不一样的值  （原生select name from stu limit 8,4;）8位置,4数量
        $sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.tip='0' limit ".$Page->firstRow.','.$Page->listRows;
        $plains = M()->query($sql);
        //var_dump($tips);exit;
        $this->assign('plains',$plains);




        $this->getTopUser();  //执行首页用户名、头像的显示12条
        $this->getTopView();  //执行最近热帖方法在页面显示10条热帖
        $this->getTopAnswer(); //执行最近热议方法在页面显示10条热议
        $this->assign('title','首页'); //分配动态的title，对应的是'首页'
        $this->display();
    }
}

?>